#include "jdtmypushbutton.h"

JdtMyPushButton::JdtMyPushButton(QWidget *parent)
    : QPushButton{parent}
{
    // 设置默认大小
    this->setFixedSize(Width, Height);

    Move_Left_To_Right_Timer = new QTimer(this);
    Move_Left_To_Right_Timer->setInterval(1);
    Move_Right_To_Left_Timer = new QTimer(this);
    Move_Right_To_Left_Timer->setInterval(1);
    Move_Pos_X = Height/2;
    Move_Pos_Y = Height/2;

    connect(Move_Left_To_Right_Timer, &QTimer::timeout, [=](){
        Move_Pos_X++;
        qDebug() << "Moving right:" << Move_Pos_X;
        if(Move_Pos_X >= Width - Height/2)
        {
            Move_Pos_X = Width - Height/2;
            Move_Left_To_Right_Timer->stop();
            Move_Flag = false;
            Right_Pos_Flag = true;
            Left_Pos_Flag = false;
            this->BackgrdColor = QColor(100, 184, 255);
            Event_Flag = 1;
        }
        this->update();
    });

    connect(Move_Right_To_Left_Timer, &QTimer::timeout, [=](){
        Move_Pos_X--;
        qDebug() << "Moving left:" << Move_Pos_X;
        if(Move_Pos_X <= Height/2)
        {
            Move_Pos_X = Height/2;
            Move_Right_To_Left_Timer->stop();
            Move_Flag = false;
            Right_Pos_Flag = false;
            Left_Pos_Flag = true;
            this->BackgrdColor = QColor(0, 0, 0);
            Event_Flag = 1;
        }
        this->update();
    });
}

void JdtMyPushButton::setSwitchSize(int width, int height)
{
    if (width < height * 2) {
        width = height * 2; // 确保宽度至少是高度的2倍
        qDebug() << "Width adjusted to:" << width << "to maintain proper aspect ratio";
    }

    this->Width = width;
    this->Height = height;
    this->setFixedSize(Width, Height);

    // 重置位置
    if (Left_Pos_Flag) {
        Move_Pos_X = Height/2;
    } else {
        Move_Pos_X = Width - Height/2;
    }
    Move_Pos_Y = Height/2;

    this->update();
}

void JdtMyPushButton::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    drawBorderOut(&painter);

    if(Event_Flag == 0 && Move_Flag == false && Left_Pos_Flag == true)
    {
        drawBorderInLeft(&painter);
    }
    if(Event_Flag == 0 && Move_Flag == false && Right_Pos_Flag == true)
    {
        drawBorderInRight(&painter);
    }
    if(Event_Flag == 1 && Move_Flag == false && Left_Pos_Flag == true)
    {
        drawCircleLeftBig(&painter);
    }
    if(Event_Flag == 1 && Move_Flag == false && Right_Pos_Flag == true)
    {
        drawCircleRightBig(&painter);
    }
    if(Move_Flag == true && Left_Pos_Flag == true)
    {
        drawCircleLeft_To_RightMove(&painter);
    }
    if(Move_Flag == true && Right_Pos_Flag == true)
    {
        drawCircleRight_To_LeftMove(&painter);
    }
}

void JdtMyPushButton::drawBorderOut(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(this->BackgrdColor);

    // 绘制左右两个半圆
    painter->drawEllipse(QPoint(Height/2, Height/2), Height/2, Height/2);
    painter->drawEllipse(QPoint(Width - Height/2, Height/2), Height/2, Height/2);

    // 绘制中间的矩形区域 - 使用矩形而不是圆角矩形，确保上下边是直线
    painter->drawRect(QRect(Height/2, 0, Width - Height, Height));

    painter->restore();
}

void JdtMyPushButton::drawBorderInLeft(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::white);
    int side = qMax(4, Height/5); // 根据高度动态计算边距
    painter->drawEllipse(QPoint(Height/2, Height/2), (Height - side)/2, (Height - side)/2);
    painter->restore();
}

void JdtMyPushButton::drawBorderInRight(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::white);
    int side = qMax(4, Height/5); // 根据高度动态计算边距
    painter->drawEllipse(QPoint(Width - Height/2, Height/2), (Height - side)/2, (Height - side)/2);
    painter->restore();
}

void JdtMyPushButton::drawCircleLeftBig(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::white);
    int side = qMax(2, Height/10); // 根据高度动态计算边距
    painter->drawEllipse(QPoint(Height/2, Height/2), (Height - side)/2, (Height - side)/2);
    painter->restore();
}

void JdtMyPushButton::drawCircleRightBig(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::white);
    int side = qMax(2, Height/10); // 根据高度动态计算边距
    painter->drawEllipse(QPoint(Width - Height/2, Height/2), (Height - side)/2, (Height - side)/2);
    painter->restore();
}

void JdtMyPushButton::drawCircleLeft_To_RightMove(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::white);
    int side = qMax(2, Height/10); // 根据高度动态计算边距
    painter->drawEllipse(QPoint(Move_Pos_X, Move_Pos_Y), (Height - side)/2, (Height - side)/2);
    painter->restore();
}

void JdtMyPushButton::drawCircleRight_To_LeftMove(QPainter *painter)
{
    painter->save();
    painter->setPen(Qt::NoPen);
    painter->setBrush(Qt::white);
    int side = qMax(2, Height/10); // 根据高度动态计算边距
    painter->drawEllipse(QPoint(Move_Pos_X, Move_Pos_Y), (Height - side)/2, (Height - side)/2);
    painter->restore();
}

bool JdtMyPushButton::event(QEvent *event)
{
    switch (event->type())
    {
    case QEvent::Leave:
        Event_Flag = 0;
        this->update();
        qDebug() << "Mouse Leave" << "0";
        break;
    case QEvent::Enter:
        Event_Flag = 1;
        this->update();
        qDebug() << "Mouse Enter" << "1";
        break;
    case QEvent::MouseButtonPress:
        Event_Flag = 2;
        Move_Flag = true;
        if(Left_Pos_Flag == true)
            Move_Left_To_Right_Timer->start();
        if(Right_Pos_Flag == true)
            Move_Right_To_Left_Timer->start();
        qDebug() << "Mouse click" << "2";
        emit clicked();
        break;
    case QEvent::MouseButtonRelease:
        qDebug() << "Mouse Release" << "3";
        break;
    default:
        break;
    }
    return QPushButton::event(event);
}
